package com.fduss.artStore.dao.impl;

import com.fduss.artStore.dao.TypesShipperDao;
import com.fduss.artStore.entity.TypesShipper;
import com.fduss.artStore.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by alex on 28/06/2017.
 */
public class TypesShipperDaoImpl implements TypesShipperDao {
    private JdbcUtil util;

    public TypesShipperDaoImpl() {
        util = new JdbcUtil();
    }

    @Override
    public TypesShipper add(TypesShipper typesShipper) {
        Connection conn = util.getConnection();
        String sql = "INSERT INTO `art`.`typesshippers` " +
                "(`shipperID`, `shipperName`, `shipperDescription`, `shipperAvgTime`, " +
                "`shipperClass`, `shipperBaseFee`, `shipperWeightFee`) VALUES (?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement pst = null;
        boolean flag = false;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesShipper.getShipperId());
            pst.setString(2, typesShipper.getShipperName());
            pst.setString(3, typesShipper.getShipperDescription());
            pst.setString(4, typesShipper.getShipperAvgTime());
            pst.setInt(5, typesShipper.getShipperClass());
            pst.setBigDecimal(6, typesShipper.getShipperBaseFee());
            pst.setBigDecimal(7, typesShipper.getShipperWeightFee());
            pst.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }

        return flag ? typesShipper : null;
    }

    @Override
    public void delete(int typesShipperId) {
        Connection conn = util.getConnection();
        String sql = "DELETE FROM `art`.`typesshippers` WHERE `shipperID` = ?";
        PreparedStatement pst = null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesShipperId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }

    }

    @Override
    public TypesShipper getOne(int typesShipperId) {
        Connection conn = util.getConnection();
        //language=MySQL
        String sql = "SELECT * FROM `art`.`typesshippers` WHERE `shipperID` = ?";
        PreparedStatement pst = null;
        ResultSet rs = null;
        TypesShipper shipper = null;
        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesShipperId);
            rs = pst.executeQuery();

            if (rs.next()) {
                shipper = buildShipper(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }
        return shipper;
    }

    @Override
    public List<TypesShipper> getAll() {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`typesshippers`";
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<TypesShipper> shippers = new ArrayList<>();

        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();

            while (rs.next()) {
                shippers.add(buildShipper(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }
        return shippers;
    }

    private TypesShipper buildShipper(ResultSet rs) throws SQLException {
        TypesShipper shipper = new TypesShipper();
        shipper.setShipperAvgTime(rs.getString("shipperAvgTime"));
        shipper.setShipperBaseFee(rs.getBigDecimal("shipperBaseFee"));
        shipper.setShipperClass(rs.getInt("shipperClass"));
        shipper.setShipperDescription(rs.getString("shipperDescription"));
        shipper.setShipperId(rs.getInt("shipperID"));
        shipper.setShipperName(rs.getString("shipperName"));
        shipper.setShipperWeightFee(rs.getBigDecimal("shipperWeightFee"));
        return shipper;
    }
}
